<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>es6</title>
</head>
<body>
<script>
    const name1 = "闷墩儿";
    const age1 = 2;
    // const dog = { name: name, age: age };
    const dog1 = { name1, age1 };

    const name2 = "闷墩儿";
    // const dog2 = {
    //     run: function () {
    //         return name2 + "在公园里奔跑！";
    //     },
    // };
    const dog2 = {
        run() {
            return `${name2}在公园里奔跑！`;
        },
    };

    const key1 = `name`;
    const dog4 = {
        [key1]: "闷墩儿",
    };

    let obj1 = { species: "柯基", name: "闷墩儿", age: 2 };
    let obj2 = { ...obj1 };
    console.log(obj2);
    console.log("\n");

    let obj3 = { species: "柯基", name: "闷墩儿", age: 2 };
    let obj4 = { food: "狗粮" };
    let obj5 = { ...obj3, ...obj4 };
    console.log(obj5);
    console.log("\n");

    console.log(-0 == +0); // true
    console.log(-0 === +0); // true
    console.log(NaN == NaN); // false
    console.log(NaN === NaN); // false
    console.log(7 == "7"); // true
    console.log("\n");

    console.log(Object.is(-0, +0)); // false
    console.log(Object.is(NaN, NaN)); // true
    console.log(Object.is(7 === "7")); // false
    console.log("\n");

    const obj6 = { name: "闷墩儿", food: "狗粮" };
    const obj7 = { age: 2, hobby: "跑圈圈" };
    const obj8 = { food: "鸡胸肉", color: "黑白黄" };
    Object.assign(obj6, obj7, obj8); // 将 obj2 和 obj3 合并到 obj1 中
    console.log(obj6); // 浅拷贝
    obj7.hobby = "游泳";
    console.log(obj7);
    console.log(obj6);
    console.log("\n");

    let s1 = new Set();
    s1.add(1);
    s1.add(2);
    s1.add(2);
    s1.add(3);
    s1.add(3);
    console.log(s1); // {1,2,3}
    console.log(s1.size);
    console.log("\n");

    let colors = new Set(["Green", "Red", "Orange"]);
    console.log(colors);
    console.log("\n");

    let dogs1 = new Set(["柯基", "博美", "比熊"]);
    dogs1.forEach(function details(values) {
        console.log(`Hello，我是一只小${values}`);
    });
    console.log("\n");

    let dogs2 = new Set(["柯基", "博美", "比熊"]);
    // 回调函数
    function details(values) {
        console.log(`Hello，我是一只老${values}`);
    }
    dogs2.forEach(details);
    console.log("\n");

    let s2 = new Set();
    let obj9 = {};
    let obj10 = {};
    s2.add(obj9);
    s2.add(obj10);
    console.log(s2.size); // 2
    console.log(s2);
    obj9 = null;
    console.log(s2.size); // 2
    console.log(s2);
    console.log("\n");

    let s3 = new WeakSet();
    let obj11 = {a:1,b:2};
    let obj12 = {c:3};
    s3.add(obj11);
    s3.add(obj12);
    console.log(s3.size);
    console.log(s3);
    obj11 = null;
    console.log(s3.size);
    console.log(s3);
    console.log("\n");

    let bookstore = new Map();
    bookstore.set([1, 2, 3], "书籍");
    bookstore.set(false, "日用品");
    bookstore.set(3, "化妆品");
    console.log(bookstore);
    console.log(bookstore.get(3));
    console.log(bookstore.has(false));
    bookstore.delete(3);
    console.log(bookstore.has(3));
    bookstore.clear();
    console.log(bookstore);
    console.log("\n");

    let userName = new Map([
        [1, "小红"],
        [2, "小蓝"],
        [3, "小白"],
    ]);
    userName.forEach(function (value, key, ownerMap) {
        console.log(`userName 中的值有： ${value}`);
        console.log(`userName 中的键有：${key}`);
        console.log(ownerMap);
    });
    console.log("\n");

    // var outnum1 = function (n, callback) {
    //     setTimeout(function () {
    //         console.log(n);
    //         callback();
    //     }, 1000);
    // };
    // outnum1("1", function () {
    //     outnum1("2", function () {
    //         outnum1("3", function () {
    //             console.log("0");
    //         });
    //     });
    // });
    var outnum2 = function (n) {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                console.log(n);
                resolve();
            }, 1000);
        });
    };
    outnum2("1")
        .then(function () {
            return outnum2("2");
        })
        .then(function () {
            return outnum2("3");
        })
        .then(function () {
            console.log("0");
            console.log("\n");
        });
    // console.log("\n");

    let n = 6;
    let p1 = new Promise(function (resolve, reject) {
        setTimeout(function () {
            if (n > 5) {
                resolve(n);
            } else {
                reject("必须大于5");
            }
        },4000);
    });
    p1.then(
        function (v) {
            console.log("成功：" + v);
            console.log("\n");
        },
        function (e) {
            console.log("失败：" + e);
            console.log("\n");
        }
    );
    // 执行代码后，由于 n 值大于 5 ，因此，在控制台中输出数字 6 。

    function p2(n) {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                if (n > 0) {
                    resolve(n);
                } else {
                    reject("不能小于0");
                }
            }, 5000);
        });
    }
    Promise.all([p2(5), p2(6), p2(7)]).then( //三个执行成功的任务
        function (v) {
            console.log("成功：" + v);
            console.log("\n");
        },
        function (e) {
            console.log("失败：" + e);
            console.log("\n");
        }
    );
    Promise.all([p2(5), p2(-2), p2(7)]).then( //一个执行失败的任务，二个执行成功的任务
        function (v) {
            console.log("成功：" + v);
            console.log("\n");
        },
        function (e) {
            console.log("失败：" + e);
            console.log("\n");
        }
    );

    let timeOut = new Promise(function (resolve, reject) {
        setTimeout(function () {
            reject("请求超时");
        }, 7000);
    });
    function loadData() {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve("请求成功");
            }, 6000);
        });
    }
    Promise.race([loadData(), timeOut]).then(
        function (d) {
            console.log(d);
            console.log("\n");
        },
        function (e) {
            console.log(e);
            console.log("\n");
        }
    );

    var p3 = function (msg) {
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                resolve(msg);
            }, 3000);
        });
    };
    p3("汪思韵")
        .then(function (v) {
            return p3(v + "，男"); // 3
        })
        .then(function (v) {
            return p3(v + "，今年22岁"); // 6
        })
        .then(function (v) {
            console.log(v); // 9
            console.log("\n");
        })

    async function fn1() {
        return "666";
    }
    fn1().then((val) => {
        console.log(val);
        console.log("\n");
    });

    // 函数 p 返回的是一个 Promise 对象，在对象中，延时 10 秒，执行成功回调函数，相当于模拟一次异步请求
    function p4(v) {
        return new Promise(function (resolve) {
            setTimeout(function () {
                // 在 p 函数执行时，将函数的实参值 v ，作为执行成功回调函数的返回值。
                resolve(v);
            }, 10000);
        });
    }
    // 一个用于正常输出内容的函数
    function log() {
        console.log("2.正在操作");
        console.log("\n");
    }
    async function fn2() {
        console.log("1.开始");
        await log();
        let yb = await p4("3.异步请求");
        console.log(yb);
        console.log("4.结束");
        console.log("\n");
    }
    fn2();

    // 函数 p 返回的是一个 Promise 对象，在对象中，延时 2 秒，执行成功回调函数，相当于模拟一次异步请求
    function p5(v) {
        return new Promise(function (resolve) {
            setTimeout(function () {
                // 在 p 函数执行时，将函数的实参值 v ，作为执行成功回调函数的返回值。
                resolve(v);
            }, 4000);
        });
    }
    async function fn3() {
        let p1 = await p5("汪思韵");
        let p2 = await p5(p1 + "，男");
        let p3 = await p5(p2 + "，今年22岁");
        console.log(p3);
        console.log("登录成功！");
        console.log("\n");
    }
    fn3();

    // 函数 p 返回的是一个 Promise 对象，在对象中，延时 2 秒，执行成功回调函数，相当于模拟一次异步请求
    function p6(v) {
        return new Promise(function (resolve) {
            setTimeout(function () {
                // 在 p 函数执行时，将函数的实参值 v ，作为执行成功回调函数的返回值。
                resolve(v);
            }, 4300);
        });
    }
    // async function fn4() { // 并发
    //     await Promise.all([p6("a"), p6("b"), p6("c")]);
    //     console.log("隐藏加载动画！");
    // }
    async function fn4() { // 顺序
        await p6("a");
        await p6("b");
        await p6("c");
        console.log("隐藏加载动画！");
    }
    fn4();
</script>
</body>
</html>